package com.application.sistercityapplication.main;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.application.sistercityapplication.dto.DashBoardDto;
import com.application.sistercityapplication.dto.MagazineDto;
import com.application.sistercityapplication.dto.NewsDto;
import com.application.sistercityapplication.json.JSONParserForGetList;
import com.application.sistercityapplication.model.Magazine;
import com.application.sistercityapplication.news.FullNewsMainActivity;
import com.application.sistercityapplication.utils.DateUtil;
import com.application.sistercityapplication.utils.DownloadTask;
import com.application.sistercityapplication.utils.RoundedTransformation;
import com.artifex.mupdfdemo.MuPDFActivity;
import com.devsmart.android.ui.HorizontalListView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.roscopeco.ormdroid.Entity;
import com.squareup.picasso.Picasso;
import com.todddavies.components.progressbar.ProgressWheel;

@SuppressLint({ "ValidFragment", "CutPasteId" })
public class MainPageFragment extends Fragment {
	private View v, v2;
	private int cID;
	private FrameLayout mainLayout;
	private LayoutInflater inflater2;
	private ListView lv;
	private List<MagazineDto> magList;
	private List<NewsDto> listNews;
	private String country_title;
	private SlidingMenu sm;
	private NewsMainPageAdapter nmp;
	private DocumentMainPageAdapter dmp;
	private KmsPageAdapter kmsAdapter;
	private MouPageAdapter mouAdapter;
	private DashBoardDto dashBoardDto;
	private List<MagazineDto> kmsList;
	private List<MagazineDto> mouList;
	private DashBoardAdapter dashBoardAdapter;
	private Activity activity;
	private TextView countryDesc;
	public MainPageFragment(int country_id,String country_title,SlidingMenu sm) {
		this.cID = country_id;
		this.country_title = country_title;
		this.sm= sm;
	}
	public MainPageFragment(int country_id,String country_title,SlidingMenu sm,Activity activity) {
		this.cID = country_id;
		this.country_title = country_title;
		this.sm= sm;
		this.activity = activity;
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

		StrictMode.setThreadPolicy(policy);
		v = inflater.inflate(R.layout.new_activity_city_information_main,container, false);

		mainLayout = (FrameLayout) v.findViewById(R.id.frameForInflate);
		inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v2 = inflater2.inflate(R.layout.layout_dashboard_page, null);
		lv = (ListView) v2.findViewById(R.id.listView1);
		mainLayout.addView(v2);
		TextView actionHeaderName = (TextView) v2.findViewById(R.id.textView1);
		ImageView menuBtn = (ImageView) v2.findViewById(R.id.progress);
		ImageView back_icon = (ImageView) v2.findViewById(R.id.back_icon);
		ImageView imgFlag = (ImageView) v2.findViewById(R.id.imageView1);
		countryDesc = (TextView) v2.findViewById(R.id.textView2);
		
		new InitialDashBoardPage().execute();
		
		actionHeaderName.setText(getResources().getString(R.string.country_text)+country_title);
		menuBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sm.toggle();
			}
		});
		back_icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getActivity().finish();
			}
		});
		int drawableFlag;
		switch (cID) {
		case 2:
			drawableFlag = R.drawable.new_brunai;
			break;
		case 3:
			drawableFlag = R.drawable.new_cambodia;
			break;
		case 4:
			drawableFlag = R.drawable.new_indonesia;
			break;
		case 5:
			drawableFlag = R.drawable.new_lao;
			break;
		case 6:
			drawableFlag = R.drawable.new_malaysia;
			break;
		case 7:
			drawableFlag = R.drawable.new_philipine;
			break;
		case 8:
			drawableFlag = R.drawable.new_singapore;
			break;
		case 9:
			drawableFlag = R.drawable.new_thailand;
			break;
		case 10:
			drawableFlag = R.drawable.new_vietnam;
			break;
		case 11:
			drawableFlag = R.drawable.new_myanmar;
			break;
		default:
			drawableFlag = R.drawable.new_thailand;
			break;
		}
		Picasso.with(getActivity().getApplicationContext()).load(drawableFlag).into(imgFlag);
		
		
		return v;
	}

	public class InitialDashBoardPage extends AsyncTask<String, Void, List<NewsDto>> {
		ProgressDialog Asycdialog = new ProgressDialog(getActivity());
		String typeStatus;
		
		
		@Override
		protected void onPreExecute() {
			// set message of the dialog
			Asycdialog.setMessage("Get Content Loading...");
			// show dialog
			Asycdialog.show();
			super.onPreExecute();
		}

		@Override
		protected List<NewsDto> doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			JSONParserForGetList json = JSONParserForGetList.getInstance();
			dashBoardDto =  json.getDashBoard(cID+"");
			listNews = dashBoardDto.getNewsList();
			magList = dashBoardDto.getMagList();
			kmsList = dashBoardDto.getKmsList();
			mouList = dashBoardDto.getMouList();
			
			return listNews;
		}

		@Override
		protected void onPostExecute(List<NewsDto> result) {
			// hide the dialog
			Asycdialog.dismiss();
			if(result!=null){
				countryDesc.setText(dashBoardDto.getDescription());
				dashBoardAdapter = new DashBoardAdapter();
				lv.setAdapter(dashBoardAdapter);
			}else {
				(Toast.makeText(getActivity().getApplicationContext(), "An error occured.", Toast.LENGTH_LONG)).show();
			}
		}

	}

	public class DashBoardAdapter extends BaseAdapter {
		private List<Integer> _listSize;
		private ViewHolderForFirst viewForFirst;
		public DashBoardAdapter(){
			_listSize = new ArrayList<Integer>();
			for(int i =0;i<4;i++){
				_listSize.add(i);
			}
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return _listSize.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return _listSize.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			if(position==0){
					viewForFirst = new ViewHolderForFirst();
					LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					
					convertView = inflater.inflate(R.layout.main_page_notice_news, null);
					viewForFirst.listView = (HorizontalListView) convertView.findViewById(R.id.listview);
					viewForFirst.eachboxname = (TextView) convertView.findViewById(R.id.textView1);
					viewForFirst.icon_img = (ImageView) convertView.findViewById(R.id.icon_img);
					nmp = new NewsMainPageAdapter();
					viewForFirst.listView.setAdapter(nmp);
					viewForFirst.eachboxname.setText(getResources().getString(R.string.menu_list_news));
					viewForFirst.more = (TextView) convertView.findViewById(R.id.textView2);
					Picasso.with(getActivity().getApplicationContext()).load(R.drawable.news).into(viewForFirst.icon_img);
					
					viewForFirst.more.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Fragment newContent = new NoticeAndNewsFragment(cID,sm,country_title);
							switchFragment(newContent);
						}
					});
					convertView.setTag(viewForFirst);
				
				
			}
			if(position==1){
					viewForFirst = new ViewHolderForFirst();
					LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					
					convertView = inflater.inflate(R.layout.main_page_notice_news, null);
					viewForFirst.listView = (HorizontalListView) convertView.findViewById(R.id.listview);
					viewForFirst.eachboxname = (TextView) convertView.findViewById(R.id.textView1);
					viewForFirst.icon_img = (ImageView) convertView.findViewById(R.id.icon_img);
					viewForFirst.more = (TextView) convertView.findViewById(R.id.textView2);
					dmp = new DocumentMainPageAdapter();
					viewForFirst.listView.setAdapter(dmp);
					viewForFirst.eachboxname.setText(getResources().getString(R.string.menu_list_document));
					Picasso.with(getActivity().getApplicationContext()).load(R.drawable.doc).into(viewForFirst.icon_img);
					
					viewForFirst.more.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Intent intent = new Intent(getActivity(),MainNewsDockViewActivity.class);
							intent.putExtra("countryId", cID);
							intent.putExtra("countryName", country_title);
							intent.putExtra("menu", 0);
							startActivity(intent);
						}
					});
					
					convertView.setTag(viewForFirst);
				
			}
			if(position==2){
					viewForFirst = new ViewHolderForFirst();
					LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					
					convertView = inflater.inflate(R.layout.main_page_notice_news, null);
					viewForFirst.listView = (HorizontalListView) convertView.findViewById(R.id.listview);
					viewForFirst.eachboxname = (TextView) convertView.findViewById(R.id.textView1);
					viewForFirst.icon_img = (ImageView) convertView.findViewById(R.id.icon_img);
					kmsAdapter = new KmsPageAdapter();
					viewForFirst.listView.setAdapter(kmsAdapter);
					viewForFirst.eachboxname.setText(getResources().getString(R.string.menu_list_mk));
					Picasso.with(getActivity().getApplicationContext()).load(R.drawable.doc).into(viewForFirst.icon_img);
					viewForFirst.more = (TextView) convertView.findViewById(R.id.textView2);
					viewForFirst.more.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Intent intent = new Intent(getActivity(),MainNewsDockViewActivity.class);
							intent.putExtra("countryId", cID);
							intent.putExtra("countryName", country_title);
							intent.putExtra("menu", 1);
							startActivity(intent);
						}
					});
					convertView.setTag(viewForFirst);
				
			}
			if(position==3){
					viewForFirst = new ViewHolderForFirst();
					LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					
					convertView = inflater.inflate(R.layout.main_page_notice_news, null);
					viewForFirst.listView = (HorizontalListView) convertView.findViewById(R.id.listview);
					viewForFirst.eachboxname = (TextView) convertView.findViewById(R.id.textView1);
					viewForFirst.icon_img = (ImageView) convertView.findViewById(R.id.icon_img);
					mouAdapter = new MouPageAdapter();
					viewForFirst.listView.setAdapter(mouAdapter);
					viewForFirst.eachboxname.setText(getResources().getString(R.string.menu_list_memo));
					Picasso.with(getActivity().getApplicationContext()).load(R.drawable.doc).into(viewForFirst.icon_img);
					viewForFirst.more = (TextView) convertView.findViewById(R.id.textView2);
					viewForFirst.more.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Intent intent = new Intent(getActivity(),MainNewsDockViewActivity.class);
							intent.putExtra("countryId", cID);
							intent.putExtra("countryName", country_title);
							intent.putExtra("menu", 2);
							startActivity(intent);
						}
					});
					convertView.setTag(viewForFirst);
				
			}
			
			return convertView;
		}

	}
	public class ViewHolderForFirst{
		private TextView actionHeaderName,eachboxname,countryDesc,more;
		private ImageView menuBtn,back_icon,icon_img,imgFlag;
		private HorizontalListView listView;
	}
	public class NewsMainPageAdapter extends BaseAdapter{
		private NewsOnlyViewHolder viewNewsOnly;
		private NewsDto newsDto;
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listNews!=null ? listNews.size() :0;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return listNews.get(position);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
//			newsDto = new NewsDto();
			if(convertView== null){
				viewNewsOnly = new NewsOnlyViewHolder();
				LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.viewitem, null);
				viewNewsOnly.title = (TextView) convertView.findViewById(R.id.Title);
				viewNewsOnly.date = (TextView) convertView.findViewById(R.id.date);
				viewNewsOnly.image = (ImageView) convertView.findViewById(R.id.image);
				
				convertView.setTag(viewNewsOnly);
			}else{
				viewNewsOnly = (NewsOnlyViewHolder) convertView.getTag();
			}
			newsDto = listNews.get(position);
			viewNewsOnly.title.setText(newsDto.getTitle());
			viewNewsOnly.date.setText(getDateStr(newsDto.getCreated_at()));
			if(newsDto.getImage_path()!=null &&!"".equals(newsDto.getImage_path())){
			
			Picasso.with(getActivity()
					.getApplicationContext())
					.load(newsDto.getImage_path())
					
					.transform(new RoundedTransformation(30, 5))
					.resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size)
					.into(viewNewsOnly.image);
			viewNewsOnly.image.setTag(position);
			
			}
			viewNewsOnly.image.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					newsDto = listNews.get((Integer) v.getTag());
					NewsDto dto = JSONParserForGetList.getInstance().getNewsDetail(newsDto.getId());
					
					Intent i = new Intent(activity,FullNewsMainActivity.class);
					i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
					i.putExtra("news", dto);
					activity.startActivity(i);
					activity.overridePendingTransition(R.anim.slide_in_from_bot, R.anim.slide_in_from_bot);
				}
			});
			
			
			return convertView;
		}
	}
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof NewDashBoardPageActivity) {
			NewDashBoardPageActivity fca = (NewDashBoardPageActivity) getActivity();
			fca.switchContent(fragment);
		}
	}
	public class NewsOnlyViewHolder{
		private TextView title,date;
		private ImageView image;
	}
	
	
	public class DocumentMainPageAdapter extends BaseAdapter{
		private DocumentView viewNewsOnly;
		private MagazineDto newsDto;
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return magList!=null ? magList.size() : 0;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return magList.get(position);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
//			newsDto = new NewsDto();
			if(convertView== null){
				viewNewsOnly = new DocumentView();
				LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.viewitem, null);
				viewNewsOnly.title = (TextView) convertView.findViewById(R.id.Title);
				viewNewsOnly.date = (TextView) convertView.findViewById(R.id.date);
				viewNewsOnly.bookClick = (RelativeLayout) convertView.findViewById(R.id.BookClick);
				viewNewsOnly.image = (ImageView) convertView.findViewById(R.id.image);
				viewNewsOnly.progressWheel = (ProgressWheel) convertView.findViewById(R.id.pw_spinner);
				convertView.setTag(viewNewsOnly);
			}else{
				viewNewsOnly = (DocumentView) convertView.getTag();
			}
			newsDto = magList.get(position);
			viewNewsOnly.title.setText(newsDto.getTitle());
			viewNewsOnly.date.setText(getDateStr(newsDto.getCreated_at()));
			if(newsDto.getTumbnail()!=null &&!"".equals(newsDto.getTumbnail())){
			Picasso.with(getActivity()
					.getApplicationContext())
					.load(newsDto.getTumbnail())
					
					.transform(new RoundedTransformation(30, 5))
					.resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size)
					.into(viewNewsOnly.image);
			}
			final Magazine mag = Entity.query(Magazine.class).where("magId").eq(newsDto.getMagId()).execute();
			if(mag!=null){
				viewNewsOnly.bookClick.setOnClickListener(new OnClickListener() {
					
				
					@Override
					public void onClick(View v) {
							Uri uri = Uri.parse(mag.path);
	
							Intent intent = new Intent(activity, MuPDFActivity.class);
	
							intent.setAction(Intent.ACTION_VIEW);
	
							intent.setData(uri);
	
							activity.startActivity(intent);
						
					}
				});
			}else{
				viewNewsOnly.bookClick.setOnClickListener(new DownloadDocumentClickListener(position,convertView,newsDto));
				
			}
			return convertView;
		}
	}
	public class KmsPageAdapter extends BaseAdapter{
		private DocumentView viewNewsOnly;
		private MagazineDto newsDto;
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return kmsList!=null?kmsList.size():0;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return kmsList.get(position);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
//			newsDto = new NewsDto();
			if(convertView== null){
				viewNewsOnly = new DocumentView();
				LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.viewitem, null);
				viewNewsOnly.title = (TextView) convertView.findViewById(R.id.Title);
				viewNewsOnly.date = (TextView) convertView.findViewById(R.id.date);
				viewNewsOnly.bookClick = (RelativeLayout) convertView.findViewById(R.id.BookClick);
				viewNewsOnly.image = (ImageView) convertView.findViewById(R.id.image);
				viewNewsOnly.progressWheel = (ProgressWheel) convertView.findViewById(R.id.pw_spinner);
				convertView.setTag(viewNewsOnly);
			}else{
				viewNewsOnly = (DocumentView) convertView.getTag();
			}
			newsDto = kmsList.get(position);
			viewNewsOnly.title.setText(newsDto.getTitle());
			viewNewsOnly.date.setText(getDateStr(newsDto.getCreated_at()));
			if(newsDto.getTumbnail()!=null &&!"".equals(newsDto.getTumbnail())){
			Picasso.with(getActivity()
					.getApplicationContext())
					.load(newsDto.getTumbnail())
					
					.transform(new RoundedTransformation(30, 5))
					.resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size)
					.into(viewNewsOnly.image);
			}
			final Magazine mag = Entity.query(Magazine.class).where("kmsId").eq(newsDto.getKmsId()).execute();
			if(mag!=null){
				viewNewsOnly.bookClick.setOnClickListener(new OnClickListener() {
					
				
					@Override
					public void onClick(View v) {
							Uri uri = Uri.parse(mag.path);
	
							Intent intent = new Intent(activity, MuPDFActivity.class);
	
							intent.setAction(Intent.ACTION_VIEW);
	
							intent.setData(uri);
	
							activity.startActivity(intent);
						
					}
				});
			}else{
				viewNewsOnly.bookClick.setOnClickListener(new DownloadDocumentClickListener(position,convertView,newsDto));
				
			}
			return convertView;
		}
	}
	public class MouPageAdapter extends BaseAdapter{
		private DocumentView viewNewsOnly;
		private MagazineDto newsDto;
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mouList!=null?mouList.size():0;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mouList.get(position);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
//			newsDto = new NewsDto();
			if(convertView== null){
				viewNewsOnly = new DocumentView();
				LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.viewitem, null);
				viewNewsOnly.title = (TextView) convertView.findViewById(R.id.Title);
				viewNewsOnly.date = (TextView) convertView.findViewById(R.id.date);
				viewNewsOnly.bookClick = (RelativeLayout) convertView.findViewById(R.id.BookClick);
				viewNewsOnly.image = (ImageView) convertView.findViewById(R.id.image);
				viewNewsOnly.progressWheel = (ProgressWheel) convertView.findViewById(R.id.pw_spinner);
				
				convertView.setTag(viewNewsOnly);
			}else{
				viewNewsOnly = (DocumentView) convertView.getTag();
			}
			newsDto = mouList.get(position);
			viewNewsOnly.title.setText(newsDto.getTitle());
			viewNewsOnly.date.setText(getDateStr(newsDto.getCreated_at()));
			if(newsDto.getTumbnail()!=null &&!"".equals(newsDto.getTumbnail())){
			Picasso.with(getActivity()
					.getApplicationContext())
					.load(newsDto.getTumbnail())
					
					.transform(new RoundedTransformation(30, 5))
					.resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size)
					.into(viewNewsOnly.image);
			}
			final Magazine mag = Entity.query(Magazine.class).where("mouId").eq(newsDto.getMouId()).execute();
			if(mag!=null){
				viewNewsOnly.bookClick.setOnClickListener(new OnClickListener() {
					
				
					@Override
					public void onClick(View v) {
							Uri uri = Uri.parse(mag.path);
	
							Intent intent = new Intent(activity, MuPDFActivity.class);
	
							intent.setAction(Intent.ACTION_VIEW);
	
							intent.setData(uri);
	
							activity.startActivity(intent);
						
					}
				});
			}else{
				viewNewsOnly.bookClick.setOnClickListener(new DownloadDocumentClickListener(position,convertView,newsDto));
				
			}
			return convertView;
		}
	}
	public class DocumentView{
		private TextView title,date;
		private ImageView image;
		private RelativeLayout bookClick;
		private ProgressWheel progressWheel;
	}
	public String getDateStr(String date){
		String dateStr = "";
		try {
			if(date!=null && !"".equals(date)){
			Date tempDate = DateUtil.convertStringToDateByFormat(date, DateUtil.DEFAULT_DATETIME_PATTERN);
			dateStr = DateUtil.toStringThaiDateFullFormat(tempDate);
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return dateStr;
	}
	public class DownloadDocumentClickListener implements OnClickListener{
		private int _position;
		private View view;
		private MagazineDto magazineDto;
		public DownloadDocumentClickListener(int position,View btnView,MagazineDto magazineDto){
			this._position = position;
			this.view = btnView;
			this.magazineDto = magazineDto;
		}
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub'
			
			//Toast.makeText(_context, String.valueOf(_position), Toast.LENGTH_SHORT).show();
			//viewHolder.downLoadBookBtn.setImageDrawable(_activity.getResources().getDrawable(R.drawable.download_temp));
			AlertDialog.Builder builder = new AlertDialog.Builder(
					activity); 
            builder.setTitle(activity.getString(R.string.download_desc));
            builder.setInverseBackgroundForced(true);
            builder.setPositiveButton(activity.getString(R.string.download_confirm),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                        	
                        	ImageView bookImage = (ImageView) view.findViewById(R.id.image);
                        	bookImage.getDrawable().mutate().setAlpha(20);
                        	bookImage.invalidate();
                        	ProgressWheel progressWheel = (ProgressWheel) view.findViewById(R.id.pw_spinner);
                        	progressWheel.setVisibility(View.VISIBLE);
                        	MagazineDto dto = new MagazineDto();
                        	if(magazineDto.getMagId()!=null){
                        		List<MagazineDto> magList = JSONParserForGetList.getInstance().getMagazines(cID+"");
                            	if(magList!=null && magList.size()>0){
                            		for(MagazineDto mDto : magList){
    	                        			if(mDto.getMagId().equals(magazineDto.getMagId())){
    	                        				dto = mDto;
    	                        			}
                            			
                            		}
                            	}
                        	}else if(magazineDto.getKmsId()!=null){
                        		List<MagazineDto> magList = JSONParserForGetList.getInstance().getKMS(cID+"");
                            	if(magList!=null && magList.size()>0){
                            		for(MagazineDto mDto : magList){
    	                        			if(mDto.getKmsId().equals(magazineDto.getKmsId())){
    	                        				dto = mDto;
    	                        			}
                            			
                            		}
                            	}
                        	}else if(magazineDto.getMouId()!=null){
                        		List<MagazineDto> magList = JSONParserForGetList.getInstance().getMOU(cID+"");
                            	if(magList!=null && magList.size()>0){
                            		for(MagazineDto mDto : magList){
    	                        			if(mDto.getMouId().equals(magazineDto.getMouId())){
    	                        				dto = mDto;
    	                        			}
                            			
                            		}
                            	}
                        	}
                        	
                        	DownloadTask downloadTask = new DownloadTask(activity,progressWheel,dto,bookImage,dashBoardAdapter);
                			downloadTask.execute(dto.getPath());
                			
                			
                        }
                    });
            builder.setNegativeButton(activity.getString(R.string.cancel),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            dialog.dismiss();
                        }
                    });
           
            AlertDialog alert = builder.create();
            alert.show();
		}
		
	}
}
